/**
 * @version	$Id: individual.js 24 2011-02-10 22:40:51Z gregdaynes@gmail.com $
 * @package	jEvolve.syncr
 * @copyright  Copyright (C) 2010 jEvolve, LLC. All rights reserved.
 * @license	GNU General Public License
 */
 
 window.addEvent('domready', function() {
 	
 	var action = function(field, newAction, disabled) {
 		if (field.tagName === 'UL') {
 			field.getParent('span').removeProperty('class').addClass('field').addClass(newAction);
 			
 			if (disabled === true) {
 				field.addClass('disabled');
 			} else {
 				field.removeClass('disabled');
 			}
 		} else {	
	 		row = field.getParent('span');
	 		row.removeProperty('class').addClass('field').addClass(newAction);
	 		
	 		if (disabled === true) {
	 			field.setProperty('disabled', 'disabled');
	 		} else {
	 			field.removeProperty('disabled');
	 		}
	 	}
 	};
 	
 	// default select | this deals with translation issues
 	defaultCategorySelect = $(document.body).getElement('[class~=categorySelect]').get('html');
 	
 	// left site select
 	$(document).getElements('[class~=siteSelect]').addEvent('change', function() {
 		
 		thisField = this;
 		
 		// remove arrow add activity indicator
 		action(this, 'activity');
 		
 		// disable category field
 		action(thisField.getParent('div').getElement('[class~=categorySelect]'), 'none', false);
 		thisField.getParent('div').getElement('[class~=categorySelect]').setHTML('');
 		
 		
 		// evaluate selection
 		if (this.getProperty('value') <= 0) {
 			console.log('Valid site not selected');
 			action(this, 'fail');
 		} else { // get site details from master contoller
			var request = new Request.JSON({
				url: 'index.php',
				onComplete: function(response) {
					
					// remove activity indicator add check
					action(thisField, 'check');
					
					// add activity indicator to category select
					action(thisField.getParent('div').getElement('[class~=categorySelect]'), 'activity');
					
					// function
					siteSelectChange(thisField);
					
				}
			}).post({
				'option':	'com_syncr',
				'task':		'getSiteDetails',
				'format':	'raw',
				'id':		this.getProperty('value')
			});
		}
 	});
 	
 	// site select change
 	var siteSelectChange = function(siteSelect) {
 		
 		// update category select list
 		var request = new Request.JSON({
 			url: 'index.php',		
 			onComplete: function(response) { 				
 				
 				siteSelect.getParent('div').getElement('[class~=categorySelect]').set('html', defaultCategorySelect);
 				
 				response.each(function(el, i) {
 					new Element('option', {
 						text: el.title,
 						value: el.id
 					}).inject(siteSelect.getParent('div').getElement('[class~=categorySelect]'));
 				});
 				
 				// enable category select
 				action(siteSelect.getParent('div').getElement('[class~=categorySelect]'), 'arrow');
 			}
 		}).post({
 			'option':		'com_syncr',
 			'controller':	'fetch',
 			'task':			'categories',
 			'format':		'raw',
 			'siteId':		siteSelect.getProperty('value')
 		});
 	};
 	
 	$(document).getElements('[class~=categorySelect]').addEvent('change', function() {
 		thisField = this;
 		
 		// remove arrow add activity indicator
 		action(thisField, 'activity');
 		
 		// disable content field
 		action(thisField.getParent('div').getElement('[class~=contentSelect]'), 'none', false);
 		thisField.getParent('div').getElement('[class~=contentSelect]').setHTML('');
 		
 		// evaluate selection
		if (this.getProperty('value') <= 0) {
			console.log('Valid category not selected');
			action(this, 'fail');
		} else { // get site details from master contoller					
			// remove activity indicator add check
			action(thisField.getParent('div').getElement('[class~=categorySelect]'), 'check');
			
			// add activity indicator to category select
			action(thisField.getParent('div').getElement('[class~=contentSelect]'), 'activity');
			
			//function
			categorySelectChange(thisField);			
		}
 	});
 	
 	var categorySelectChange = function(categorySelect) {
 		var request = new Request.JSON({
 			url: 'index.php',
 			onComplete: function(response) { 				
 				response.each(function(el, i) {
 					newItem = new Element('li', {
 					    text: el.title,
 					    id: 'c'+el.id,
 					    'events': {
 					            'click': function(){
 					                this.toggleClass('selected');
 					    		}
 					   	}
 					}).inject(categorySelect.getParent('div').getElement('[class~=contentSelect]')); 					
 				});
 				
 				// enable content select
 				action(categorySelect.getParent('div').getElement('[class~=contentSelect]'), 'arrow');
 			}
 		}).post({
 			'option':		'com_syncr',
 			'controller':	'fetch',
 			'task':			'content',
 			'format':		'raw',
 			'siteId':		categorySelect.getParent('div').getElement('[class~=siteSelect]').getProperty('value'),
 			'categoryId':	categorySelect.getProperty('value')
 		});
  	};
 	
 	/**
 	 * initialize
 	 *
 	 * disable fields
 	 * indicate site fields
 	 */
 	// disable everything
 	$(document).getElements('[class~=categorySelect]').setProperty('disabled', 'disabled');
 	$(document).getElements('[class~=contentSelect]').addClass('disabled');
 	
 	// indicate sites
 	$(document).getElements('[class~=siteSelect]').each(function(el) {
 		action(el, 'arrow');
 	});
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	var activity = function(field, newActivity) {
 		field.getChildren('span').removeProperty('class').addClass(newActivity);
 	};
 	
 	// rightwards arrow
	$('rightwardsArrow').addEvent('click', function() {
		
		// each item selected		
		$('leftPane').getElements('li[class=selected]').each(function(el, i) {
			// id of item - use id[1]
			id = el.getProperty('id').split('c');
			
			// queue
			queue = $('baseQueue').clone().setStyle('display', '');
			$(document).getElement('[class=queue]').adopt(queue);
			queue.getElement('div[class=task]').setText(el.getProperty('text'));
			
			var itemObject = new Object();
				itemObject.queue = queue;
				itemObject.processId = Math.round(new Date().getTime() / 1000) + parseInt(id[1]);

				itemObject.itemId = id[1];
				itemObject.fromSite = $('leftSiteSelect').getProperty('value');
				itemObject.toSite = $('rightSiteSelect').getProperty('value');
				itemObject.toCategory = $('rightCategorySelect').getProperty('value');
				
			requestContent(itemObject);
		});
	});
	
	
	var requestContent = function(iObj) {
		
		activity(iObj['queue'].getElement('div[class=receive]'), 'activity');
		
		var request = new Request({
			url: 'index.php',
			onSuccess: function(response) {				
				iObj.data = response;
				
				activity(iObj.queue.getElement('div[class=receive]'), 'check');
														
				receiveContent(iObj);
			}
		}).post({
			'option':		'com_syncr',
			'controller':	'fetch',
			'task':			'request',
			'format': 		'raw',
			'siteId': 		iObj.fromSite,
			'itemId':		iObj.itemId,
			'processId':	iObj.processId
			
		});
	};
	
	var receiveContent = function(iObj) {
		
		activity(iObj.queue.getElement('div[class=send]'), 'activity');
		
		var request = new Request({
			url: 'index.php',
			onComplete: function(response) {			
				activity(iObj.queue.getElement('div[class=send]'), 'check');
				
				processContent(iObj);
			}
		}).post({
			'option':		'com_syncr',
			'controller':	'fetch',
			'task':			'receive',
			'format':		'raw',
			'siteId':		iObj.toSite,
			'processId':	iObj.processId,
			'categoryId':	iObj.toCategory,
			'data':			iObj.data
		});
	};
	
	var processContent = function(iObj) {	
		activity(iObj.queue.getElement('div[class=process]'), 'activity');
		
		var request = new Request({
			url: 'index.php',
			onComplete: function(response) {
				
				activity(iObj.queue.getElement('div[class=process]'), 'check');
				
				cleanUp(iObj);
			}
		}).post({
			'option':		'com_syncr',
			'controller':	'fetch',
			'task':			'process',
			'format':		'raw',
			'siteId':		iObj.toSite,
			'processId':	iObj.processId
		});
	};
	
	var cleanUp = function(iObj) {
		var request = new Request({
			url: 'index.php',
			onComplete: function(response) {
				fadeDestroy(iObj);
			}
		}).post({
			'option':		'com_syncr',
			'controller':	'fetch',
			'task':			'cleanup',
			'format':		'raw',
			'siteId':		iObj.toSite,
			'processId':	iObj.processId
		});
		
		clearSelection = $(document.body).getElement('#clear_selection');
		
		if (clearSelection.getProperty('checked') === true) {
			$(document.body).getElements('li[class~=selected]').toggleClass('selected');
		}
	};
	
	
	
	
	var fadeDestroy = function(iObj) {
		duration = 2500;
		var el = iObj.queue;
		iObj.queue.set('tween', {
			duration: duration
		}).fade('out').get('tween').chain(function() {
			el.dispose();
		});
	};
	$(document).getElements('a[class~=delete]').addEvent('click', function(e) {
		e.stop();
		
		thisSite = this.getParent('div').getElement('[class~=siteSelect]').getProperty('value');
		
		this.getParent('div').getElement('[class~=contentSelect]').getElements('li.selected').each(function(el, i) {
			id = el.getProperty('id').split('c');
			
			itemObject = {
				'itemId': id[1],
				'task': 'delete',
				'siteId': thisSite
			};
			
			simpleTask(itemObject);
		});
	});
	
	$(document).getElements('a[class~=archive]').addEvent('click', function(e) {
		e.stop();
		
		thisSite = this.getParent('div').getElement('[class~=siteSelect]').getProperty('value');
		
		this.getParent('div').getElement('[class~=contentSelect]').getElements('li.selected').each(function(el, i) {
			id = el.getProperty('id').split('c');
			
			itemObject = {
				'itemId': id[1],
				'task': 'archive',
				'siteId': thisSite
			};
			
			simpleTask(itemObject);
		});
	});
	
	$(document).getElements('a[class~=unpublish]').addEvent('click', function(e) {
		e.stop();
		
		thisSite = this.getParent('div').getElement('[class~=siteSelect]').getProperty('value');
		
		this.getParent('div').getElement('[class~=contentSelect]').getElements('li.selected').each(function(el, i) {
			id = el.getProperty('id').split('c');
			
			itemObject = {
				'itemId': id[1],
				'task': 'unpublish',
				'siteId': thisSite
			};
			
			simpleTask(itemObject);
		});
	});
	
	$(document).getElements('a[class~=flag]').addEvent('click', function(e) {
		e.stop();
		
		thisSite = this.getParent('div').getElement('[class~=siteSelect]').getProperty('value');
		
		this.getParent('div').getElement('[class~=contentSelect]').getElements('li.selected').each(function(el, i) {
			id = el.getProperty('id').split('c');
			
			itemObject = {
				'itemId': id[1],
				'task': 'removeFlag',
				'siteId': thisSite
			};
			
			simpleTask(itemObject);
		});
	});
	
	var simpleTask = function(itemObject) {
		var request = new Request({
			url: 'index.php',
			onComplete: function(response) {
				console.log(response);
			}
		}).post({
			'option':		'com_syncr',
			'controller':	'fetch',
			'task':			'task',
			'format':		'raw',
			'siteId':		itemObject['siteId'],
			'itemId':		itemObject['itemId'],
			'action':		itemObject['task']
		});
	};
	
	// reload
	$(document.body).getElements('[class~=reload]').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			
			button = this;
			
			// clear list items
			button.getParent('div').getElement('[class~=contentSelect]').set('html', '');
			
			var request = new Request.JSON({
				url: 'index.php',
				onComplete: function(response) { 				
					response.each(function(el, i) {
						newItem = new Element('li', {
						    text: el.title,
						    id: 'c'+el.id,
						    'events': {
						            'click': function(){
						                this.toggleClass('selected');
						    		}
						   	}
						}).inject(button.getParent('div').getElement('[class~=contentSelect]')); 					
					});
					
					// enable content select
					action(button.getParent('div').getElement('[class~=contentSelect]'), 'arrow');
				}
			}).post({
				'option':		'com_syncr',
				'controller':	'fetch',
				'task':			'content',
				'format':		'raw',
				'siteId':		button.getParent('div').getElement('[class~=siteSelect]').getProperty('value'),
				'categoryId':	button.getParent('div').getElement('[class~=categorySelect]').getProperty('value')
			});
		});
	});
	
	// force load
	$(document.body).getElements('[class~=forceload]').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			
			button = this;
			
			// clear list items
			button.getParent('div').getElement('[class~=contentSelect]').set('html', '');
			
			var request = new Request.JSON({
				url: 'index.php',
				onComplete: function(response) { 				
					response.each(function(el, i) {
						newItem = new Element('li', {
						    text: el.title,
						    id: 'c'+el.id,
						    'events': {
						            'click': function(){
						                this.toggleClass('selected');
						    		}
						   	}
						}).inject(button.getParent('div').getElement('[class~=contentSelect]')); 					
					});
					
					// enable content select
					action(button.getParent('div').getElement('[class~=contentSelect]'), 'arrow');
				}
			}).post({
				'option':		'com_syncr',
				'controller':	'fetch',
				'task':			'content',
				'format':		'raw',
				'siteId':		button.getParent('div').getElement('[class~=siteSelect]').getProperty('value'),
				'categoryId':	-2
			});
		});
	});
});